package ar.com.maddes.creenciasadventistas;

import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SimpleCursorAdapter;
import ar.com.maddes.creenciasadventistas.database.Creencia;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;

public class HomeActivity extends SherlockFragmentActivity implements
		ActionBar.OnNavigationListener, LoaderManager.LoaderCallbacks<Cursor> {

	private static final int LOADER_NAVIGATION_LIST = 0;

	private ViewPager mViewPager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// Set content layout
		setContentView(R.layout.activity_home);
		
		// Delete old database if exists from previous versions:
		deleteDatabases();

		// Navigation list
		getSupportLoaderManager()
				.initLoader(LOADER_NAVIGATION_LIST, null, this);

		// Fragments pager
		mViewPager = (ViewPager) findViewById(R.id.pager);
		mViewPager.setAdapter(new FragmentPagerAdapter(
				getSupportFragmentManager()) {

			@Override
			public int getCount() {
				return getResources().getStringArray(R.array.titles).length;
			}

			@Override
			public Fragment getItem(int position) {
				Fragment fragment = new BeliefFragment();
				Bundle args = new Bundle();
				args.putInt(BeliefFragment.ARG_BELIEF_NUMBER, position);
				fragment.setArguments(args);
				return fragment;
			}
		});

		// Set the swype listener
		mViewPager
				.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
					@Override
					public void onPageSelected(int position) {
						getSupportActionBar().setSelectedNavigationItem(
								position);
					}
				});
	}

	@Override
	public boolean onNavigationItemSelected(int position, long id) {
		mViewPager.setCurrentItem(position);
		return false;
	}

	@Override
	public Loader<Cursor> onCreateLoader(int loaderId, Bundle bundle) {
		switch (loaderId) {
		case LOADER_NAVIGATION_LIST:
			return new CursorLoader(this, Creencia.CONTENT_URI, null, null,
					null, null);
		}
		return null;
	}

	@Override
	public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
		switch (loader.getId()) {
		case LOADER_NAVIGATION_LIST:
			// Specify a SimpleCursorAdapter to populate the dropdown list.
			getSupportActionBar().setListNavigationCallbacks(
					new SimpleCursorAdapter(getSupportActionBar()
							.getThemedContext(),
							android.R.layout.simple_list_item_1, cursor,
							new String[] { Creencia.TITLE },
							new int[] { android.R.id.text1 }, 0), this);
			
			// Set the navigation mode...
			getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

		}
	}

	@Override
	public void onLoaderReset(Loader<Cursor> loader) {
		// Not used?
	}
	
	public void deleteDatabases() {

		for (String database:databaseList()) {
			deleteDatabase(database);
		}
	}
}
